package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    public void saveWithFlavor(DishDTO dishDTO) {

        // 删除缓存
        String key = "categoryId:" + dishDTO.getCategoryId();
        redisTemplate.delete(key);

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        // 插入菜品
        dishMapper.insert(dish);
        // 插入口味
        if (dishDTO.getFlavors() != null && !dishDTO.getFlavors().isEmpty()) {
            dishFlavorMapper.insertBatch(dishDTO.getFlavors());
        }
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        // 分页查询
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        if (ids.isEmpty()) return;

        // 批量删除缓存
        String key = "categoryId:*";
        Set keys = redisTemplate.keys(key);
        redisTemplate.delete(keys);

        // 删除菜品
        dishMapper.deleteByIds(ids);
        // 删除口味
        dishFlavorMapper.deleteByDishIds(ids);
    }

    /**
     * 菜品起售和停售
     *
     * @param status
     * @param id
     */
    @Override
    public void status(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
        dish = dishMapper.getById(id);

        // 删除缓存
        String key = "categoryId:" + dish.getCategoryId();
        redisTemplate.delete(key);
    }

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @param dishDTO
     */
    @Override
    public void updateWithFloavor(DishDTO dishDTO) {

        // 删除缓存
        String key = "categoryId:" + dishDTO.getCategoryId(); // 删除新分类里的数据
        redisTemplate.delete(key);
        Long categoryId = dishMapper.getById(dishDTO.getId()).getCategoryId(); // 删除旧分类里的数据
        if (!categoryId.equals(dishDTO.getCategoryId())) {
            key = "categoryId:" + categoryId;
            redisTemplate.delete(key);
        }

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        // 修改菜品
        dishMapper.update(dish);

        // 修改口味
        // 删除旧的口味
        dishFlavorMapper.deleteByDishId(dish.getId());
        // 插入新口味
        for (DishFlavor flavor : dishDTO.getFlavors()) flavor.setDishId(dish.getId());
        dishFlavorMapper.insertBatch(dishDTO.getFlavors());
    }

    /**
     * @param id
     * @return
     */
    @Override
    public DishVO queryById(Long id) {
        // 查询菜品
        DishVO dishVO = dishMapper.queryById(id);
        // 查询口味
        dishVO.setFlavors(dishFlavorMapper.queryByDishId(id));
        return dishVO;
    }
}
